
// import allrender from "./allrender.js";

function Carts(){
    this.init();
}

$.extend(Carts.prototype,{
    init : function(){
        
        this.add_cars       = $(".add-cars");
        this.reduce_cars    = $(".reduce-cars");
        this.add_shopcart   = $(".add-shopcart");
        //console.log(1111);
        this.all_cats_count = $(".all-cats-count")
        //console.log(this.add_cars,this.reduce_cars);
        this.add_shopcart.on("click",$.proxy(this.addCart,this));
        this.add_shopcart.on("click",$.proxy(this.addCartSum,this));
        this.add_cars.on("click",$.proxy(this.addCartsCount,this))
        this.reduce_cars.on("click",$.proxy(this.reduceCartsCount,this))
        this.addCartSum();
    },
    addCart:function(evt){
        //console.log(111);
        var e = evt || window.event;
        var target = e.target || e.srcElement;
        //console.log(target);
        var id = $(target).attr("data-id");
        console.log(id);
        var s  = localStorage.getItem("carts");
        if(s == null){
            var a = [{
                id    : id,
                count :1
            }]
            localStorage.setItem("carts",JSON.stringify(a));

        }
        else{
            var la = JSON.parse(s);
            var has_same_id = false;
            $.each(la,function(index,item){
                if(item.id === id ){
                    item.count ++;
                    has_same_id = true;
                }
            })
            if(!has_same_id){
                la.push({
                    id : id,
                    count :1
                })
            }
            localStorage.setItem("carts",JSON.stringify(la));
        }
    },
    reduceCartsCount:function(evt){
        //console.log(11)
        var e = evt || event;
        var target = e.target || e.srcElement;
        var id = $(target).parent().attr("data-id");
        //console.log(id);
        var la = localStorage.getItem("carts");
        la = JSON.parse(la);
        //console.log(la);
        // $.each(la,function(index,item){
        //     //console.log(item.id);
        //     if(item.id == id ){
        //         console.log(item.id);
        //         item.count --;
        //         //console.log(item.goods_count);
        //         if(item.count == 0){
        //             la.splice(index,1);
        //         }
        //     }
        // })
        la.forEach(function(item,index){
            if(item.id == id){
                item.count --
                if(item.count == 0){
                    la.splice(index,1);
                }
            }
        })
        //la = JSON.stringify(la);
        la = JSON.stringify(la);
        localStorage.setItem("carts",la);
        location.reload();
        // $.ajax("./data/data1.json").then(function(res){
        //     allrender(res);
        // })
    },
    addCartsCount:function(evt){
        //console.log(2222)
        var e = evt || event;
        var target = e.target || e.srcElement;
        var id = $(target).parent().attr("data-id");
        var la = localStorage.getItem("carts");
        la = JSON.parse(la);
        $.each(la,function(index,item){
            console.log(item.id);
            if(item.id == id ){
                item.count ++;
                //console.log(item.goods_count);
            }
        })
        la = JSON.stringify(la);
        localStorage.setItem("carts",la);
        location.reload();
    },
    addCartSum:function(evt){
        var e = evt || event;
        //var target = e.target || e.event;
        var sum = 0;
        var la = localStorage.getItem("carts");
        la     = JSON.parse(la == null ? []:la);
        //console.log(la);
        $.each(la,function(index,item){
            sum += item.count;
        }) 
        $(this.all_cats_count).html(sum);
        //return sum;
    },



})


// new Carts();